﻿//========================================================= 
/**@file WaitedCommand.h
 * @brief 可等待的异步命令
 * 
 * @date 2019-03-15   22:51:54
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_WAITEDCOMMAND_H_
#define _LIBZHOUYB_WAITEDCOMMAND_H_
//--------------------------------------------------------- 
#include "../CommandDriver.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
namespace async {
//--------------------------------------------------------- 
/**
 * @brief 可等待的异步命令
 * @date 2019-03-15 22:52:43
 */
class WaitedCommand : public ICommandHandler, public RefObject
{
protected:
    async_mutex _mutex;
    Ref<ICommandHandler> _callback;
public:
    WaitedCommand(Ref<ICommandHandler> callback)
    {
        _mutex.lock();
        _callback = callback;
    }
    virtual ~WaitedCommand()
    {
        _mutex.unlock();
    }
    virtual bool OnCommand(Arguments& arg, Arguments& rlt)
    {
        if(!_callback.IsNull())
        {
            _callback->OnCommand(arg, rlt);
        }
        _mutex.unlock();
        return true;
    }
    virtual bool IsRunable() const { return true; }
    void Wait()
    {
        _mutex.lock();
    }
    bool Wait(uint timeoutMs)
    {
        return _mutex.lock_timeout(timeoutMs);
    }
};
//--------------------------------------------------------- 
} // namespace async
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_WAITEDCOMMAND_H_
//========================================================= 